import { HashService } from '@app/hash';
import { PrismaService } from '@app/prisma';
import { Injectable } from '@nestjs/common';

@Injectable()
export class StudentService {
    constructor(
        private readonly prisma: PrismaService,
        private readonly hashService: HashService
    ) { }

    public findOrCreateCollege(
        name: string
    ) {
        return this.prisma.college.upsert({
            where: {
                name
            },
            create: {
                name
            },
            update: {}
        })
    }
    public findOrCreateDepartmentToCollege(
        collegeId: number,
        name: string
    ) {
        const hash = this.hashService.cryptoIdAndName(collegeId, name);
        return this.prisma.department.upsert({
            where: {
                hash
            },
            update: {
                college: {
                    connect: {
                        id: collegeId
                    }
                }
            },
            create: {
                hash,
                name,
                college: {
                    connect: {
                        id: collegeId
                    }
                }
            }
        })
    }
    public findOrCreateScience(
        departmentId: number,
        name: string
    ) {
        const hash = this.hashService.cryptoIdAndName(departmentId, name)
        return this.prisma.science.upsert({
            where: {
                hash
            },
            update: {
                department: {
                    connect: {
                        id: departmentId
                    }
                }
            },
            create: {
                hash,
                name,
                department: {
                    connect: {
                        id: departmentId
                    }
                }
            }
        })
    }
    public findOrCreateClasses(
        scienceId: number,
        name: string
    ) {
        return this.prisma.classes.upsert({
            where: {
                name
            },
            update: {
                science: {
                    connect: {
                        id: scienceId
                    }
                }
            },
            create: {
                name,
                science: {
                    connect: {
                        id: scienceId
                    }
                }
            }
        })
    }
    public findOrCreateUser(
        account: string,
        name: string,
        classesId: number
    ) {
        return this.prisma.user.upsert({
            where: {
                account
            },
            update: {
                profile: {
                    update: {
                        name
                    }
                },
                classes: {
                    connect: {
                        id: classesId
                    }
                }
            },
            create: {
                account,
                classes: {
                    connect: {
                        id: classesId
                    }
                },
                hash_key: this.hashService.createUid([account]),
                profile: {
                    create: {
                        name,
                        password: this.hashService.cryptoPassword(account)
                    }
                }
            },
            include: {
                profile: true
            }
        })
    }
}
